package com.purchase.ml.resource.prod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.purchase.ml.annotations.OperLog;
import com.purchase.ml.bean.base.ApiRestResult;
import com.purchase.ml.bean.page.Page;
import com.purchase.ml.bean.req.dealer.DealerProdEditDto;
import com.purchase.ml.bean.req.dealer.DealerProdQueryDto;
import com.purchase.ml.bean.req.prod.*;
import com.purchase.ml.bean.res.prod.SysProdRes;
import com.purchase.ml.bean.res.prod.SysShopProdRes;
import com.purchase.ml.consts.PermissionsConst;
import com.purchase.ml.entity.prod.SysCategory;
import com.purchase.ml.entity.prod.SysProduct;
import com.purchase.ml.entity.prod.SysProductPic;
import com.purchase.ml.entity.prod.SysProductSku;
import com.purchase.ml.service.prod.SysCategoryService;
import com.purchase.ml.service.prod.SysProdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author :zxm
 * @date :2021/2/25 18:41
 * @description :some description
 **/
@RestController
@RequestMapping("/product")
@Slf4j
@Api(tags = "商品类")
public class ProdResource {

    @Autowired
    private SysProdService sysProdService;

    @PostMapping("/new")
    @ApiOperation("新增商品")
    @RequiresPermissions(PermissionsConst.PRODUCT_NEW)
    @OperLog(operDesc = "CreateProd", operModul = "Prod", operType = "Create")
    public ApiRestResult createProduct(@RequestBody @Valid SysProdNewDto prodNewDto){
        ApiRestResult result = sysProdService.createProduct(prodNewDto);
        return result;
    }

    @PostMapping("/edit")
    @ApiOperation("修改商品")
    @RequiresPermissions(PermissionsConst.PRODUCT_EDIT)
    @OperLog(operDesc = "EditProd", operModul = "Prod", operType = "Edit")
    public ApiRestResult editProduct(@RequestBody @Valid SysProdEditDto prodEditDto){
        ApiRestResult result = sysProdService.editProd(prodEditDto);
        return result;
    }

    @PostMapping("/query")
    @ApiOperation("分页查询商品")
    @RequiresPermissions(PermissionsConst.PRODUCT_QUERY)
    public ApiRestResult prodPage(@RequestBody SysProdQueryDto prodQueryDto){
        IPage<SysProduct> result = sysProdService.prodPage(prodQueryDto);
        return ApiRestResult.ok(result);
    }

    @PostMapping("/queryShop")
    @ApiOperation("分页查询商店商品")
    public ApiRestResult shopProdPage(@RequestBody SysProdQueryDto prodQueryDto){
        Page<SysShopProdRes> result = sysProdService.shopProdPage(prodQueryDto);
        return ApiRestResult.ok(result);
    }

    @PostMapping("/query_shop_details")
    @ApiOperation("查询商店商品详情")
    public ApiRestResult shopProdDetails(@Valid @RequestBody SysProdDto dto){
        SysProduct product = sysProdService.selectProdDetailsById(dto.getId());
        return ApiRestResult.ok(product);
    }

    @PostMapping("/delete")
    @ApiOperation("删除商品")
    @OperLog(operDesc = "DeleteProd", operModul = "Prod", operType = "Delete")
    public ApiRestResult deleteProd(@RequestBody List<Long> ids){
        sysProdService.batchDeleteProd(ids);
        return ApiRestResult.ok();
    }
}
